﻿using Coldairarrow.Entity.Base_leasesystem_agent;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base_leasesystem_agent
{
    public class leasesystem_agentBusiness : BaseBusiness<leasesystem_agent>, Ileasesystem_agentBusiness, ITransientDependency
    {
        public leasesystem_agentBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<leasesystem_agent>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<leasesystem_agent>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<leasesystem_agent, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<leasesystem_agent> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task<string> AddDataAsync(leasesystem_agent data)
        {
            await InsertAsync(data);
            return data.Id;
                
             
        }

        public async Task<string> UpdateDataAsync(leasesystem_agent data)
        {
            if (data.CheckStatus == 1) {
                Base_User user = new Base_User();
                user.Id = IdHelper.GetId();
                user.CreateTime = data.CreateTime;
                user.CreatorId = data.CreatorId;
                user.Deleted = false;
                user.UserName = data.Phone;
                user.Password = "666888".ToMD5String();
                user.RealName = data.AgentName;
                user.Sex = Sex.Man;
                user.telephone = data.Phone;
                user.brandId = data.Id;
                user.brandName = data.AgentName;
                await Db.InsertAsync(user);


                Base_UserRole ur = new Base_UserRole();
                ur.Id = IdHelper.GetId();
                ur.CreateTime = data.CreateTime;
                ur.CreatorId = data.CreatorId;
                ur.Deleted = false;
                ur.UserId = user.Id;
                ur.RoleId = "1514160123348520960";
            }
            await UpdateAsync(data);
            return data.Id;
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public object GetAgent_Select()
        {
            var query = Db.GetIQueryable<leasesystem_agent>().Select(a => new
            {
                a.Id,
                a.AgentName
            });
            return query;
        }

        #endregion

        #region 私有成员

        #endregion
    }
}